package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.AbnormalConsumptionDTO;
import com.quanquan.management.dto.ActivityApplyDTO;
import com.quanquan.management.dto.ActivityDTO;
import com.quanquan.management.dto.SelfHelpIntegralDTO;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.request.ActivityApplyRequestBody;
import com.quanquan.management.request.ActivityRequest;
import entity.ExcelData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;
import utils.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述:活动管理页面逻辑层
 *
 * @ClassName: ActivityService
 * @Author: xiaoqianh丶
 * @Date: 2019-04-07 15:36
 * @Version: V1.0
 */
@Service
public class ActivityService {

    @Autowired
    private SystemLogService systemLogService;

    @Value("${module-url.storeUrl}")
    private String storeUrl;


    public PageDTO<ActivityDTO> listActivitys(ActivityRequest pageRequestBody) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String remark = "查看活动列表数据";
        systemLogService.sendSystemLog(sysUser, remark);

        String json = Request.postJson(storeUrl + "activity/query_management", pageRequestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<ActivityDTO> pageDTO = null;
        if (0 == resCode) {
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            return data;
        }
        return pageDTO;
    }


    public boolean addActivity(ActivityRequest requestBody) {

        if (null == requestBody) {
            return false;
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String remark = "添加新数据";
        systemLogService.sendSystemLog(sysUser, remark);
        String json = Request.postJson(storeUrl + "activity/add", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        ActivityDTO activityDTO = null;
        if (0 == resCode) {

            return true;

        }
        return false;
    }

    public ActivityDTO findById(String id) {
        ActivityRequest requestBody = new ActivityRequest();
        requestBody.setActivityId(id);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());

        String remark = "查看活动详情数据";
        systemLogService.sendSystemLog(sysUser, remark);

        String json = Request.postJson(storeUrl + "activity/query_by_id", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        ActivityDTO activityDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            activityDTO = data.toJavaObject(ActivityDTO.class);
        }
        return activityDTO;
    }

    public boolean update(ActivityRequest requestBody) {
        if (null == requestBody) {
            return false;
        }
        requestBody.setActivityId(requestBody.getId());
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());

        String remark = "更新活动数据";
        systemLogService.sendSystemLog(sysUser, remark);

        String json = Request.postJson(storeUrl + "activity/update_activity", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean delete(String id) {
        ActivityRequest requestBody = new ActivityRequest();
        requestBody.setActivityId(id);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());
        String remark = "删除活动数据";
        systemLogService.sendSystemLog(sysUser, remark);

        String json = Request.postJson(storeUrl + "activity/remove_activity", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return resCode == 0;
    }

    public List<ActivityDTO> list() {
        ActivityRequest activityRequest = new ActivityRequest();
        String postJson = Request.postJson(storeUrl + "activity/list", activityRequest);
        JSONObject jsonObject = JSONObject.parseObject(postJson);
        Integer resCode = jsonObject.getInteger("resCode");
        List<ActivityDTO> pageDTO = null;
        if (0 == resCode) {
            return jsonObject.getJSONArray("data").toJavaList(ActivityDTO.class);
        }
        return pageDTO;
    }


    /**
     * 导出所有活动
     *
     * @param
     * @param response
     */
    public void export(HttpServletResponse response, ActivityApplyRequestBody requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("手机号");
        titles.add("用户名");
        titles.add("报名时间");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<ActivityApplyDTO> pageDTO = this.listActivityApplyRecords(requestBody);
        List<ActivityApplyDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        for (ActivityApplyDTO vo : list){
            List<Object> row = new LinkedList<>();
            row.add(vo.getPhone());
            row.add(vo.getMemberName());
            row.add(vo.getCreateTime());
            rows.add(row);
        }
        /*list.forEach(vo -> {
            List<Object> row = new LinkedList<>();
            row.add(vo.getPhone());
            row.add(vo.getMemberName());
            row.add(vo.getCreateTime());
            rows.add(row);
        });*/
        data.setRows(rows);
        String now = DateUtils.currentDate(DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response, "活动报名记录" + now, data);

    }

    public PageDTO<ActivityApplyDTO> listActivityApplyRecords(ActivityApplyRequestBody pageRequestBody) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String remark = "查看活动报名数据";
        systemLogService.sendSystemLog(sysUser, remark);

        String postJson = Request.postJson(storeUrl + "/activity_apply/find_all", pageRequestBody);
        PageDTO<ActivityApplyDTO> pageDTO = null;
        if (StringUtil.isEmpty(postJson)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(postJson);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<ActivityApplyDTO> userDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                ActivityApplyDTO object = list.getObject(i, ActivityApplyDTO .class);
                userDTOS.add(object);
            }
            data.setList(userDTOS);
            return data;
        }
        return pageDTO;
    }
}
